import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Message extends StatefulWidget {
  _ContentWidgetState createState() => new _ContentWidgetState();
}

class _ContentWidgetState extends State<Message> {
/**
 * 
BinaryCodec	          发送二进制消息时
JSONMessageCodec	    发送Json格式消息时
StandardMessageCodec	发送基本型数据时
StringCodec	          发送String类型消息时
 */
  static const _channel =
      BasicMessageChannel('channel_message', StandardMessageCodec());

  Future<dynamic> _platformCallHandler(dynamic message) async {
    print('flutter收到原生消息 = $message');
  }

  void _sendMessage() async {
    final String reply = await _channel.send('flutter发送消息');
    print(reply);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _sendMessage();

    _channel.setMessageHandler(_platformCallHandler);
  }

  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: AppBar(
          title: Text('Flutter消息页面'),
        ),
        body: Center(
          child: Center(
            child: Text('Flutter消息页面'),
          ),
        ));
  }
}
